package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *     角色管理
 * </p>
 * @Author：Super
 * @Date: 2020/12/4 15:03
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    //创建日志对象
    private static final Logger log = LoggerFactory.getLogger(RestController.class);



    @Reference
    private RoleService roleService;



    /**
     * 角色管理的分页条件查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        //远程调用roleService的方法分页查询
        PageResult<Role> pageResult = roleService.findPage(queryPageBean);

        return new Result(true, "查询角色信息列表成功。",pageResult);
    }


    /**
     * 查询所有权限
     * @return
     */
    @GetMapping("/findAllPermission")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findAllPermission(){

        List<Permission> permissionList = roleService.findAllPermission();

        return new Result(true,"查询所有权限成功",permissionList);

    }


    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("/findAllMenu")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findAllMenu(){

        List<Menu> menuList = roleService.findAllMenu();

        return new Result(true,"查询所有菜单成功",menuList);
    }


    /**
     * 添加角色
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Map<String,Object> param){

        roleService.add(param);

        return new Result(true,"新增角色成功");
    }


    /**
     * 删除角色
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result deleteById(Integer id){

        roleService.deleteById(id);

        return new Result(true,"删除角色成功");

    }


    /**
     * 修改角色数据回显--查询角色
     * @param id
     * @return
     */
    @GetMapping("/findRoleById")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result findRoleById(Integer id){

        Role role = roleService.findRoleById(id);

        return new Result(true,"查询角色成功",role);
    }


    /**
     * 查询和角色对应的权限
     * @return
     */
    @GetMapping("/findPermissionIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPermissionIdsByRoleId(Integer id){

        List<Integer> permissionList = roleService.findPermissionIdsByRoleId(id);

        return new Result(true,"查询和角色对应的权限成功",permissionList);
    }


    /**
     * 查询和角色对应的菜单
     * @param id
     * @return
     */
    @GetMapping("/findMenuIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findMenuIdsByRoleId(Integer id){

        List<Integer> menuList = roleService.findMenuIdsByRoleId(id);

        return new Result(true,"查询和角色对应的菜单成功",menuList);

    }


    /**
     * 编辑角色
     * @param param
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result update(@RequestBody Map<String,Object> param){

        roleService.update(param);

        return new Result(true,"编辑角色信息成功");



    }

    @RequestMapping("findAll")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findAll() {
        List<Role> roleList = roleService.findAll();
        return new Result(true, "查询角色列表成功", roleList);
    }
}
